#ifndef DSD_HPP
#define DSD_HPP

#include <cwchar>
#include <iostream>
#include <algorithm>
#include <mockturtle/mockturtle.hpp>
#include <vector>
#include <alice/alice.hpp>
#include "detail_dsd.hpp"

namespace alice
{
	class decomp_flow_command : public command
	{
	public:
		explicit decomp_flow_command(const environment::ptr &env) : command(env, "perform disjoint-support decomposition")
		{
			add_option("--tt, -a", tt_in, "dsd from hex");
			add_option("--binary, -b", tt_in, "dsd from binary");
			add_flag("--noM3aj, -m", "don't ues M3aj,default = true");
			add_flag("--noM5aj, -w", "don't ues M5aj,default = true");
			add_flag("--noXor, -x", "don't ues Xor,default = true");
			add_flag("--noMux, -u", "don't ues Mux,default = true");
			add_flag("--shortsupport, -s", "The decomposition stops when the number of variables <= 4, default = false ");
		}

	protected:
		void execute()
		{
			mockturtle::klut_network ntk;
			std::vector<mockturtle::klut_network::signal> children;
			kitty::dynamic_truth_table remainder;
			mockturtle::decomposition_flow_params ps;

			if (is_set("tt"))
			{
				mockturtle::read_hax(tt_in, remainder, var_num, ntk, children);
			}
			else if (is_set("binary"))
			{
				mockturtle::read_binary(tt_in, remainder, var_num, ntk, children);
			}
			else
			{
				if (is_set("noM3aj"))
				{
					ps.allow_m3aj = false;
				}
				if (is_set("noM5aj"))
				{
					ps.allow_m5aj = false;
				}
				if (is_set("noXor"))
				{
					ps.allow_xor = false;
				}
				if (is_set("noMux"))
				{
					ps.allow_mux = false;
				}
				if (is_set("shortsupport"))
				{
					ps.allow_s = true;
				}
				ntk.create_po(mockturtle::dsd_detail(ntk, remainder, children, ps));
				write_bench(ntk, "decomp.bench");
			}
		}

	private:
		std::string tt_in;
		int var_num;
	};
	ALICE_ADD_COMMAND(decomp_flow, "decomposition");
}

#endif
